package com.itheima.dao.impl;

import com.itheima.dao.IStudentDao;
import com.itheima.domain.Student;

import java.util.ArrayList;

public class StudentDaoImpl implements IStudentDao {

    private static ArrayList<Student> students = new ArrayList<Student>();

    static {
        Student student1 = new Student("0001", "蔡徐坤", 27, "1994");
        Student student2 = new Student("0002", "黄子韬", 26, "1995");
        Student student3 = new Student("0003", "鹿晗", 28, "1993");
        Student student4 = new Student("0004", "吴亦凡", 30, "1991");
        students.add(student1);
        students.add(student2);
        students.add(student3);
        students.add(student4);
    }
    @Override
    public void add(Student student) {
        students.add(student);
    }

    @Override
    public void delte(String sid) {
        int index = getIndex(sid);
        students.remove(index);
    }

    @Override
    public void update(String sid, Student student) {
        int index = getIndex(sid);
        students.set(index,student);
    }

    @Override
    public ArrayList<Student> findAll() {
        return students;
    }

    @Override
    public int getIndex(String sid) {
        if (students == null || students.size() == 0) {
            return -1;
        }
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            if (student != null && student.getSid().equals(sid)) {
                return i;
            }
        }
        return -1;
    }
}
